//
//  DoctorApplyController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "DoctorApplyController.h"
#import "DoctorApplyListModel.h"
#import "ViewController.h"
#import "DoctorApplicationCell.h"
#import "DoctorDetailsViewController.h"

#import "UIView+Positioning.h"
#import "AnimatedTableCell.h"
#define tableViewCellHeight 100
static NSString* CELL_LIST = @"celldoctor";

@interface DoctorApplyController ()

@property(nonatomic,strong)DoctorDetailsViewController *detailVC;

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,strong)NSMutableArray* arrM;


@end

@implementation DoctorApplyController

- (void)setupUI {
  
    
    //加载tableView
    [self createTableView];
    
    self.table = self.tableView;
    
    //加载数据
    [self addData];
}
#pragma mark
#pragma mark - 界面将要出现
-(void)viewWillAppear:(BOOL)animated
{
    UIBarButtonItem *btnReset = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(replayAnimation)];
    self.navigationItem.rightBarButtonItem = btnReset;
    
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
#pragma mark
#pragma mark - 导航栏Item点击事件
- (void) replayAnimation
{
    [self startTableViewAnimation:_table];
    
}
#pragma mark
#pragma mark - 执行方法

- (void) startTableViewAnimation:(UITableView *)table
{
    tableAnimated = YES;
    for (AnimatedTableCell *atCell in table.visibleCells) {
        if ([table.visibleCells indexOfObject:atCell] % 2 == 0)
            [atCell pushCellWithAnimation:YES direction:@"left"];
        else
            [atCell pushCellWithAnimation:YES direction:@"right"];
    }
}



#pragma mark
#pragma mark - 加载数据
- (void)addData
{
    [DoctorApplyListModel getListDataModelComplete:^(NSArray<DoctorApplyListModel *> *modelList, NSError *error) {
        
        self.modelList = modelList;
        
        //        [self initailStatus];
        
        [self.tableView reloadData];
    }];
}

#pragma mark
#pragma mark - 搭建界面
- (void)createTableView
{
    //创建uitableView
    [self createTableViewWithStyle:UITableViewStylePlain];
    
    
}


#pragma mark
#pragma mark - 设置代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelList.count;
}



- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    DoctorApplicationCell* cell = [tableView dequeueReusableCellWithIdentifier:CELL_LIST];
    
    if (cell == nil) {
        cell = [[DoctorApplicationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_LIST];
        
        [cell configureCellContentSizeWidth:tableView.frame.size.width height:tableViewCellHeight];
        
        
    }
    
    [cell resetPosition];
    
    
    
    
    
    // Trigger tableView didLoad and then start animation
    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
        // Show once only
        if (!tableAnimated) [self startTableViewAnimation:tableView];
    }
    //model
    DoctorApplyListModel *model = self.modelList[indexPath.row];
    
    
    cell.model = model;
    return cell;
    
}

#pragma mark
#pragma mark - 点击Cell跳转界面!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //     DoctorApplicationCell *selectedCell = (DoctorApplicationCell *)[tableView cellForRowAtIndexPath:indexPath];
    //    [selectedCell popCellWithAnimation:YES];
    
    //点击cell
    DoctorDetailsViewController * detailsVC = [[DoctorDetailsViewController alloc] init];
    //保存
    _detailVC = detailsVC;
    
    detailsVC.model = self.modelList[indexPath.row];
    //    NSLog(@"%@",detailsVC.model);
    DoctorApplicationCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    detailsVC.cell = cell;
    
    //循环存储
    
    [self.navigationController pushViewController:detailsVC animated:YES];
    
}

#pragma mark -
#pragma mark TableView Delegate Methods

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return tableViewCellHeight;
}
@end
